/*
 * Javlov - a Java toolkit for reinforcement learning with multi-agent support.
 * 
 * Copyright (c) 2009 Matthijs Snel
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package net.javlov;

import java.io.Serializable;

/**
 * The state-value function that stores the perceived value of states.
 * 
 * @author Matthijs Snel
 *
 */
public interface ValueFunction extends Serializable {

	/**
	 * Adds the provided delta to the current value of the specified state.
	 * 
	 * @param s the state of which to update the value.
	 * @param delta the delta that will be added to the current value.
	 */
	public <T> void update(State<T> s, double delta);
	
	/**
	 * Adds the provided delta to the current value of the state that was last queried
	 * using {@link #getValue(State)}.
	 * 
	 * @param delta the delta that will be added to the current value of the last queried
	 * state.
	 */
	public void update(double delta);
	
	/**
	 * Adds the provided delta to the current value of the next-to-last queried state
	 * using {@link #getValue(State)} (optional operation).
	 * 
	 * @param delta the delta that will be added to the current value of the 
	 * next-to-last queried state.
	 */
	public void updatePrevious(double delta);
	
	/**
	 * Gets the value of the specified state. The decision of what to return if the value of
	 * the state is unknown (the state hasn't been encountered before) is left up to the
	 * implementing classes.
	 * 
	 * @param s the state of which to retrieve the value.
	 * @return the value of the state.
	 */
	public <T> double getValue(State<T> s);
	
	/**
	 * Sets the value of the specified state. Any stored value for this state will be replaced
	 * with the new one.
	 * 
	 * @param s the state of which to set the value.
	 * @param value the value of the state.
	 */
	public <T> void setValue(State<T> s, double value);
	
	/**
	 * Called at the beginning of an experiment.
	 */
	public void init();
	
	/**
	 * Called whenever the learning scenario is reset without clearing the learned values,
	 * e.g. at the beginning of a new episode in episodic tasks.
	 */
	public void reset();
	
	/**
	 * 
	 * @return single parameter representing {@code gamma*lambda} for eligibility traces.
	 */
	public double getTraceDecay();
	
	/**
	 * Single parameter representing {@code gamma*lambda} for eligibility traces. Set to 0
	 * for learning without eligibility traces.
	 * 
	 * This method is on the value function instead of on the agent since
	 * 1. For function approximators the function gradient is needed to update the traces,
	 * which the agent doesn't have access to.
	 * 2. For tabular functions, multiple updates need to take place (for states with
	 * positive trace), whereas for FAs, only a single update needs to take place. By
	 * placing the trace updates within the value function, only a single agent implementation
	 * is needed.
	 */
	public void setTraceDecay(double decay);
}
